package com.atguigu.springcloud.controller;

import com.atguigu.springcloud.entity.CommentResult;
import com.atguigu.springcloud.entity.Payment;
import com.atguigu.springcloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author jxau_scf
 * @date 2021/8/26 - 20:23
 */
@RestController
@Slf4j
public class PaymentController {
    @Resource
    private PaymentService paymentService;

    @Value("${server.port}")
    private String serverPort;

    @PostMapping("/payment/create")
    public CommentResult create(@RequestBody Payment payment){
        int result = paymentService.create(payment);
        log.info("****插入结果：" + payment);
        if (result > 0){
            return new CommentResult(200,"插入数据成功,serverPort:" + serverPort, result);
        }else {
            return new CommentResult(444,"插入失败", null);
        }

    }

    @GetMapping("/payment/getPaymentById/{id}")
    public CommentResult getPaymentById(@PathVariable("id") Long id){
        Payment payment = paymentService.getPaymentById(id);
        log.info("****查询结果：" + payment);
        if (payment != null){
            return new CommentResult(200, "查询成功,serverPort:" + serverPort, payment);
        }else {
            return new CommentResult(444,"查询失败,查询Id:" + id, null);
        }

    }

    @GetMapping("/payment/lb")
    public String getPaymentLB(){
        return serverPort;
    }

}
